<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-row style="margin-bottom: 5px;">
        <el-col>
          <el-button icon="el-icon-refresh" plain size="mini" type="info" @click="refresh"
          >
            刷新
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{textAlign: 'center'}"
        border
        row-key="id"
        style="width: 100%;"
      >
        <el-table-column
          :index="indexMethod"
          align="center"
          label="序号"
          type="index"
          width="60"
        />
        <el-table-column
          label="账号"
          prop="logonName"
          width="100"
        />
        <el-table-column
          label="用户姓名"
          prop="displayName"
          width="100"
        />
        <el-table-column
          label="所在机构"
          prop="orgName"
          width="120"
        />
        <el-table-column
          label="Token"
          prop="token"
        />
        <el-table-column label="操作" width="90px">
          <template slot-scope="scope">
            <el-button
              plain
              size="mini"
              type="danger"
              icon="el-icon-circle-close"
              @click="forceLogout(scope.$index, scope.row)"
            >下线
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { get } from '@/api/base-api'

export default {
  name: 'OnlineUserManagement',
  data() {
    return {
      loading: false,
      tableData: []
    }
  },
  watch: {},
  created() {
  },
  mounted() {
    this.initData()
  },
  destroyed() {
  },
  methods: {
    indexMethod(index) {
      return index + 1
    },
    initData: function() {
      this.loading = true
      get('UserManagement/GetOnlineUsers', null).then(response => {
        this.tableData = response.list
        this.loading = false
      })
    },
    forceLogout: function(index, row) {
      this.$confirm('确定要将用户下线吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (row.isSupperAdmin) {
          this.$message({ showClose: true, message: '不可下线超级用户', type: 'error' })
          return
        } else {
          get('UserManagement/ForceLogout/' + row.token, null).then(response => {
            this.$message({ showClose: true, message: '下线成功', type: 'success' })
            this.initData()
          })
        }
      })
    },
    refresh: function() {
      this.initData()
    }
  }
}
</script>

<style scoped>

</style>
